<?php

namespace app\adminapi\controller;

use app\adminapi\validate\LoginValidate;
use app\common\library\util\Captcha;
use app\common\logic\system\AdminLogic;
use app\common\service\JsonService;

class Login extends AdminBase
{
    protected $notNeedLogin = ['*'];

    /**
     * 获取图形验证码
     * @return \think\response\Json
     */
    public function getCaptcha()
    {

        return JsonService::data((new Captcha())->create());
    }

    /**
     * 账号密码登录
     * @return \think\response\Json
     */
    public function checkAccountLogin()
    {

        $params = (new LoginValidate())->fromPost()->toCheck();
        //验证码验证
        if (true !== ($res = (new Captcha())->check($params['code'], $params['key']))) {
            return JsonService::error($res);
        }

        $token = (new AdminLogic())->login($params);
        return JsonService::success('登录成功',['token' => $token]);
    }

    public function demo()
    {
        return (new AdminLogic())->encryptPassword('123456');
    }
}